package com.dk.service.impl;

import com.dk.mapper.CarMessageMapper;
import com.dk.mapper.MessageLabelMapper;
import com.dk.pojo.CarMessage;
import com.dk.pojo.CarMessageVO;
import com.dk.pojo.MessageLabel;
import com.dk.service.CarMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CarMessageServiceImpl implements CarMessageService {

    @Autowired
    private CarMessageMapper carMessageMapper;

    @Autowired
    private MessageLabelMapper messageLabelMapper;

    @Override
    public List<CarMessageVO> toSelect() { return carMessageMapper.toSelectList(); }

    @Override
    public boolean toInsert(CarMessage carMessage) {
        int insert = carMessageMapper.insert(carMessage);
        String labelId = carMessage.getLabelId();
        String[] split = labelId.split(",");
        for (int i = 0; i < split.length; i++) {
            long l = new Long(split[i]);
            MessageLabel messageLabel = new MessageLabel();
            messageLabel.setMessageId(carMessage.getMessageId());
            messageLabel.setLabel(l);
            messageLabelMapper.insert(messageLabel);
        }
        return insert > 0;
    }

    @Override
    public CarMessage toSelectById(Long messageId) {
        return carMessageMapper.selectById(messageId);
    }

    @Override
    public boolean toUpdate(CarMessage carMessage) {
        carMessage.setStute(0);
        return carMessageMapper.updateById(carMessage) > 0;
    }
}
